package components.dialogs
{
	import UIFree.uif_component;
	
	import classes.Commands.c_applyMatrix3D;
	
	import components.editor.c_baseElement;
	
	import flash.events.Event;
	import flash.geom.Matrix3D;
	import flash.geom.Point;
	import flash.geom.Vector3D;
	
	public class c_rotateDialog extends c_elementPropDlg {
		private var _prevMatrix: Matrix3D;
		public function c_rotateDialog(parent:uif_component, a_element: c_baseElement, a_title:String='') {
			_prevMatrix = getMatrixStart(a_element);
			super(parent, a_element, propertyList(a_element), a_title);
		}
		
		protected static function getMatrixStart(a_element: c_baseElement): Matrix3D {
			var mat: Matrix3D;
			if (a_element.transform.matrix3D) mat = a_element.transform.matrix3D.clone();
			else {
				mat = new Matrix3D();
				mat.appendScale(a_element.scaleX, a_element.scaleY, 1);
				mat.appendRotation(a_element.rotation, Vector3D.Z_AXIS);
				mat.appendTranslation(a_element.x, a_element.y, 0);
			}
			return mat;
		}
		
		protected function propertyList(a_element: c_baseElement): Array {
			return [
				{
					title	: 'Вращение по Y',
					type	: c_propertyDialog.PTYPE_NUMBER,
					value	: a_element.rotationY / 360 * 100
				},{
					title	: 'Вращение по X',
					type	: c_propertyDialog.PTYPE_NUMBER,
					value	: a_element.rotationX / 360 * 100
				}
			];
		}
		
		protected function getMatrix(): Matrix3D {
			var mat: Matrix3D = new Matrix3D();
			var cen: Point = element.center;
			mat.appendTranslation(-cen.x, -cen.y, 0);
			mat.appendRotation(values[0].value / 100 * 360, Vector3D.Y_AXIS);
			mat.appendRotation(values[1].value / 100 * 360, Vector3D.X_AXIS);
			mat.appendTranslation(cen.x, cen.y, 0);
			mat.append(_prevMatrix);
			return mat;
		}
		
		override protected function updateElement(): void {
			element.transform.matrix3D = getMatrix();
		}
		
		override protected function doCancel():void {
			element.transform.matrix3D = _prevMatrix;
		}
		
		override protected function doApply():void {
			executeCommand(new c_applyMatrix3D(element, getMatrix(), _prevMatrix));
		}
	}
}